#include "r_cg_macrodriver.h"
#include "r_cg_cgc.h"
#include "r_cg_port.h"
#include "r_cg_timer.h"
#include "r_cg_pclbuz.h"
#include "r_cg_digtalLED.h"
#include "r_cg_KeyScan.h"
#include "r_cg_lcd.h"
#include "stdio.h"
#include "musicplayer.h"
#include "r_cg_wdt.h"

int num_keyboard=0,kzz=0;
int songs[93]={1,0,1,0,5,0,5,0,6,0,6,0,5,0,0,0,4,0,4,0,3,0,3,0,2,0,2,0,1,0,0, 0,5,0,5,0,4,0,4,0,3,0,3,0,2,0,0,0,5,0,5,0,4,0,4,0,3,0,3,0,2, 0,0,0,  1,0,1,0,5,0,5,0,6,0,6,0,5,0,0,0,  4,0,4,0,3,0,3,0,2,0,2,0,1};
int songs2[110]={5,0,3,0,5,0,3,0,0,5,0,3,0,1, 0,0,2,0,4,0,3,0,2,0,5,5, 0,5,0,3,0,5,0,3,0,5,0,3,0,1,1, 0,0,2,0,4,0,3,0,2,0,1,1, 0,0,2,0,2,0,4,0,4,0,3,0,1,0,5,5, 0,0,2,0,4,0,3,0,2,0,5,5, 0,0,5,0,3,0,5,0,3,0,5,0,3,0,1,1, 0,0,2,0,5,0,3,0,2,0,1,1};
extern int RLmode;

void music_play(void)
{  num_keyboard=0;
    lcd_write(0x01,0);
       // LcdFill_REC3();
	//delayb(1000);
	//delayb(1000);
	//LcdClear();
	//lcd_write(0x01,0);
	//WDTE = 0xAC;

  while(1U)
  {
	 lcd_display(0,"    choose    "
			"    1.song1     "
			"    2.song2     " );
			//"");
	    num_keyboard=Keyboard_scan()-'0';
	    switch(num_keyboard)
		
           {
	        case(1):
	           {   for(kzz=0;kzz<93;kzz++)
	                {
	                 music_player(songs[kzz]);
	                 //music_player(kzz);
			  R_WDT_Restart();
	                }
		      num_keyboard=0;
		    }     
	           break;
		   case(2):
	           {   for(kzz=0;kzz<110;kzz++)
	                {
	                 music_player(songs2[kzz]);
			  R_WDT_Restart();
	                 //music_player(kzz);
	                }
		      num_keyboard=0;
		    }     
	           break;
		   case(9):  
		   
	           break;
			 
	  }   
	   if(num_keyboard==0){num_keyboard=0;lcd_write(0x01,0); RLmode=0; break;}
	  
  }
}



void music_player(int sound)
{int i,j,k,hz=4;
 switch(sound)
 {
   case 0:
   for(k=0;k<523/hz;k++)
    { 
      R_PCLBUZ0_Stop();
      for(i=0;i<4*191;i++);
       R_PCLBUZ0_Stop();
      for(i=0;i<4*191;i++);     
    }
    break;
   case 1:
   for(k=0;k<523/hz;k++)
    { 
      R_PCLBUZ0_Start();
      for(i=0;i<8*191;i++);
       R_PCLBUZ0_Stop();
      for(i=0;i<8*191;i++);     
    }
    break;
     case 2:
   for(k=0;k<587/hz;k++)
    { 
      R_PCLBUZ0_Start();
      for(i=0;i<8*170;i++);
       R_PCLBUZ0_Stop();
      for(i=0;i<8*170;i++);     
    }
     break;
     case 3:
   for(k=0;k<659/hz;k++)
    { 
      R_PCLBUZ0_Start();
      for(i=0;i<8*152;i++);
       R_PCLBUZ0_Stop();
      for(i=0;i<8*152;i++);     
    }
     break;
      case 4:
   for(k=0;k<698/hz;k++)
    { 
      R_PCLBUZ0_Start();
      for(i=0;i<8*143;i++);
       R_PCLBUZ0_Stop();
      for(i=0;i<8*143;i++);     
    }
     break;
      case 5:
   for(k=0;k<784/hz;k++)
    { 
      R_PCLBUZ0_Start();
      for(i=0;i<8*128;i++);
       R_PCLBUZ0_Stop();
      for(i=0;i<8*128;i++);     
    }
     break;
     case 6:
   for(k=0;k<880/hz;k++)
    { 
      R_PCLBUZ0_Start();
      for(i=0;i<8*114;i++);
       R_PCLBUZ0_Stop();
      for(i=0;i<8*114;i++);     
    }
     break;
     case 7:
   for(k=0;k<988/hz;k++)
    { 
      R_PCLBUZ0_Start();
      for(i=0;i<8*101;i++);
       R_PCLBUZ0_Stop();
      for(i=0;i<8*101;i++);     
    }
     break;
 }
    
    
  
}

